package site.suimu.controller;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import site.suimu.pojo.Tnew;
import site.suimu.service.TnewService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/new")
public class TnewController {

    @Resource
    TnewService tnewService;

    /*
    * 添加新闻
    * */
    @RequestMapping(value = "/add")
    @ResponseBody
    public String addNew(Tnew tnew){
        if(tnewService.insNew(tnew)){
            return "success";
        }
        return "failed";
    }

    /*
    * 根据id修改新闻
    * */
    @RequestMapping(value = "/update")
    @ResponseBody
    public String updNew(Tnew tnew){
        if(tnewService.updNew(tnew)){
            return "success";
        }
        return "failed";
    }

    /*
    * 根据type获取新闻列表
    * */
    @RequestMapping(value = "/list")
    @ResponseBody
    public String newList(@RequestParam("type") int type){
        Map map = new HashMap<String, ArrayList>();
        map.put("list",tnewService.sleByType(type));
        return map.toString();
    }

    /*
    * 根据id查询新闻的详细信息
    * */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public String newDetail(@RequestParam("nId") int nId){
        Map map = new HashMap<String, Tnew>();
        map.put("newDetail",tnewService.sleById(nId));
        return map.toString();
    }

    /*
    * 根据id删除新闻
    * */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public String delete(@RequestParam("nId") int nId){
        if(tnewService.dleNew(nId)){
            return "success";
        }
        return "failed";
    }
}
